import scrapy


class QuotesNextPageSpider(scrapy.Spider):
    name = "quotes_next_page"

    def start_requests(self):
        url = "https://quotes.toscrape.com/page/1"
        yield scrapy.Request(url)

    def parse(self, response):
        print(f"请求页面", response.url)
        quotes = response.css('div.quote')
        for quote in quotes:
            content = quote.css('.text::text').extract_first()
            author = quote.css('.author::text').extract_first()
            tags = quote.css('.tags .tag::text').extract()

            print({
                "content": content,
                "author": author,
                "tags": tags,
            })

        next_page_url = response.css('.next>a::attr("href")').extract_first()
        if next_page_url:
            # 手动
            # next_page_url = f"https://quotes.toscrape.com{next_page_url}"
            # urljoin
            # next_page_url = response.urljoin(next_page_url)
            # print(next_page_url)
            # yield scrapy.Request(url=next_page_url)

            yield response.follow(next_page_url)

